<?php

namespace App\Http\Middleware;

use App\Http\Services\jwtAuth;
use App\Models\Personal;
use Closure;
use Illuminate\Http\Request;

class XinZhuMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse)  $next
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request, Closure $next)
    {
        $token = $request->token;
        $jwt = jwtAuth::getInstance();
        try {
            $uid = $jwt->decode($token)['uid'];
            //查询所在企业ID
            $user = Personal::select('es_id')->where('id',$uid)->first();
            $request->eid = $user->es_id;
            $request->uid = $uid;
            return $next($request);
        } catch (\Exception $e) {
            return response()->json(['code' => '401', 'msg' => '请登录']);
        }
    }
}
